package com.company.sendemo.basics.yyPersonalSummary;


import java.util.Arrays;

//java代码复制 方法
//1,system.arraycopy();
//2,Arrays.copyOf();
//3,clone();
//4,for 循环
public class TestArraysCopy {
    //数组长度不管是多少 ，单位不管是百，千，万 system。arraycopy 效率最高
    //for循环适合小型数组（单位百） 随着数组长度增加 ，效率越来越多 且单位达到万 效率最低


    //1,system.arraycopy();
    public static void testSystemArraysCopy(String[] orginal){
        long start_time = System.nanoTime();
        String[] target = new String[orginal.length];
        System.arraycopy(orginal,0,target,0,target.length);
        long end_time = System.nanoTime();
        System.out.println("使用system.arraycopy()方法耗时："+(end_time - start_time));
    }

    //2,Arrays.copyOf();
    public static void testArraysCopyOf(String[] orginal){
        long start_time = System.nanoTime();
        String[] target = new String[orginal.length];
        target = Arrays.copyOf(orginal,orginal.length);
        long end_time = System.nanoTime();
        System.out.println("使用Arrays.copyOf()方法耗时："+(end_time - start_time));
    }

    //3,clone();
    public static void testClone(String[] orginal){
        long start_time = System.nanoTime();
        String[] target = new String[orginal.length];
        target = orginal.clone();
        long end_time = System.nanoTime();
        System.out.println("使用system.arraycopy()方法耗时："+(end_time - start_time));
    }

    //4,for
    public static void testFor(String[] orginal){
        long start_time = System.nanoTime();
        String[] target = new String[orginal.length];
        for (int i = 0; i < orginal.length; i++) {
            target[i] = orginal[i];
        }
        long end_time = System.nanoTime();
        System.out.println("使用system.arraycopy()方法耗时："+(end_time - start_time));
    }

    public static void main(String[] args) {
        //需要修改変原始数组的大小
        String[] original = new String[100];
        for (int i = 0; i < original.length; i++) {
            original[i] = "abcd";
        }
        System.out.println("原始数据的大小："+original.length);

        testSystemArraysCopy(original);
        testArraysCopyOf(original);
        testClone(original);
        testFor(original);
    }

}
